package com.code.lin;


import com.code.AuthorizationUtils;

import java.io.File;
import java.text.SimpleDateFormat;

/**
 * Created by lyj on 2017/7/8.
 */
public class LicenseGenerator {

    /**
     * serial：由客户提供
     * timeEnd：过期时间
     */
   /* private static String licensestatic = "serial=604a-6aa1-5a90-7efa-05f5-0b42-8e18-8849;" +
            "timeEnd=1499616000000";*/

    private static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCF0WxWaeKNwdv4A5sfVg+2NKcplb/nUWIIbrFn\n" +
            "GXJflVnfi8heghif3Smlh5djLT4/NpmukyMV8dkeOjoqbRfvUxteVH2vhToMF5oNehRx8MdjAxEB\n" +
            "m2CuouLgiZE/gjLhnKCEUXo2DQEVtomPm9P+XZbf7bEU0cV+6fYQU9pGxwIDAQAB";

    /**
     * RSA算法
     * 公钥和私钥是一对，此处只用私钥加密
     */
    public static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIXRbFZp4o3B2/gDmx9WD7Y0pymV\n" +
            "v+dRYghusWcZcl+VWd+LyF6CGJ/dKaWHl2MtPj82ma6TIxXx2R46OiptF+9TG15Ufa+FOgwXmg16\n" +
            "FHHwx2MDEQGbYK6i4uCJkT+CMuGcoIRRejYNARW2iY+b0/5dlt/tsRTRxX7p9hBT2kbHAgMBAAEC\n" +
            "gYBDe9tgsME5r9vYdSkTYnlF78OlKwm+A8Kmcfww/Bo7rbvHCl0fnAgDBqtr+LcVoslTY5uY+Ean\n" +
            "WHGf1slif+WjZrhyc0DOa0aCso1xlrYkfgw66Axr4DjGDRsCQE1biWL7xLa4dEehv4IFQwPQL+qP\n" +
            "HXv3hUgbaHvks39pC9JHgQJBAMCIzLC506xPyJDtdmZ+nza4L4fjXL0P/kFEPf5G/EsKbe/H7xiJ\n" +
            "/dRZAad44baWYw1+vNveQ5nCPXQf97FXkSECQQCx7cpTejq3vX43P0uVl++p7UxEBIOiP7k2Z7jP\n" +
            "nIZN3WK+wCiyCUEDArrNoq3V6eG069anrWo9tB1yZXO3YhLnAkBp2syvXXvDgRoElIpET9m5fAql\n" +
            "65tRVzmzKJHs+5o6p8FJ0+Lg2v1zgnwmV4/Bk21iPgwX1lvUbbbb4beNCVgBAkABY9owzMOoq9JQ\n" +
            "nC36/TTiHvh5j493tQe9ZRIMjSx/bztWQioPOgNIAr1y3dJ92VXqE+2ZYcFyufWA70n4JMwBAkBW\n" +
            "HhfpbMQhvOUCzy2cIMDYaYrnkEHG/U4IBjsh8CL7EeMRMMVmPVmEH+TRBJCco4Zlk9Jx4raG+phO\n" +
            "utOjHTbS";

    public static void generator() throws Exception {
        String code = AuthorizationUtils.authCode();
        System.err.println("私钥加密——公钥解密");
        //String source = "568b8fa5cdfd8a2623bda1d8ab7b7b34";

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String licensestatic = "serial="+code+";timeEnd="+sf.parse("2017-07-10").getTime();

        System.out.println("原文字：\r\n" + licensestatic);
        byte[] data = licensestatic.getBytes();
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        System.out.println("加密后：\r\n" + new String(encodedData)); //加密后乱码是正常的

        Base64Utils.byteArrayToFile(encodedData, FileUtil.getBasePath()+ File.separator+"license.dat");
        System.out.println("com.code.license.dat：\r\n" + FileUtil.getBasePath()+ File.separator+"license.dat");
        byte[] newEncodedData = Base64Utils.fileToByte(FileUtil.getBasePath()+ File.separator+"license.dat");
        //Base64 base64 = new Base64();
        //byte[] newBase= base64.decodeBase64(new String(encodedData));
        //解密
        //byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        byte[] decodedData = RSAUtils.decryptByPublicKey(newEncodedData, publicKey);
        String target = new String(decodedData);
        System.out.println("解密后: \r\n" + target);
    }


    public static byte[] encrypt(String linc) throws Exception {
        System.out.println("原文字：\r\n" + linc);
        byte[] data = linc.getBytes();
        byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);
        System.out.println("加密后：\r\n" + new String(encodedData)); //加密后乱码是正常的
        return encodedData;
    }

    public static String decrypt(byte[] encodedData) throws Exception{
        byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
        String target = new String(decodedData);
        //System.out.println("解密后: \r\n" + target);
        return target;
    }

    /*public static void main(String[] args) throws Exception {
        generator();
    }*/
}
